# -*- coding: utf-8 -*-
from time import sleep

from PIL import Image

from chaojiying import Chaojiying_Client
from selenium import  webdriver
from selenium.webdriver.firefox.webdriver import Options

class cldul_chaoji():

    def __init__(self):
        #初始化和超级鹰的接口
        self.username='111111'  #用户名
        self.password='123123'#密码
        self.code_num=9103
        self.chaojiying=Chaojiying_Client(self.username,self.password, '96001')

    #获取url中的连接进行分解操作

    def replisa_img(self,url):
        #进行初始化webdriver
        opt=Options()
        #opt.add_argument('--headless')
        opt.add_argument('--disable-gpu')
        bro=webdriver.Firefox(firefox_options=opt)
        bro.get(url)
        #页面截图
        #bro.save_screenshot('img.png')
        bro.find_element_by_xpath('//*[@id="btnSubmit"]').click()
        sleep(3)
        bro.save_screenshot('img.png') #获取弹出来的验证吗截图
        code_img_ele = bro.find_element_by_xpath('/html/body/div[3]/div[2]') #获取验证码位置
        location = code_img_ele.location #获取xy轴
        print(location)
        size=code_img_ele.size #获取宽和高
        print(size)
        rangle = (
            int(location['x']), int(location['y']), int(location['x'] + size['width']),
            int(location['y'] + size['height'])
        )
        print(rangle)
        i = Image.open('img.png')
        code_img_name = './code.png'
        frame = i.crop(rangle)
        frame.save(code_img_name)

        bro.quit()

aa=cldul_chaoji()
aa.replisa_img('https://callback.58.com/antibot/verifycode?serialId=d1e87b57fc2580aa5315f8e5b162d037_22be961c23e24a88b7020ec76742cd04&code=100&sign=faa3acd9d22e11938c96be185f3623f1&namespace=ershoufanglistphp&url=https://yt.58.com/ershoufang/p2/%3E')

